import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Camino de forma curva
 * 
 * @Daniel Varela
 * @1.0
 */
public class Curva extends Camino
{
    private int x;
    private long t;

    /**
     * Constructor de la clase
     * @param pos es la rotacion de la imagen del camino
     */
    public Curva(int pos){
        super(pos);
        this.turn((pos-2)*90);
        x = 0;
        t = 0;
    }

    public void act(){
        
        if(Greenfoot.mouseClicked(this) && !super.dameBool()){
            this.rota();
        }
        
        if( super.dameBool() && !super.checaPiedra()){
            this.correLava();  
        }
        
        if(x==8){
            this.correSig();
        }
    } 
    
    /** 
     * Gira la imagen el objeto
     */
    public void rota(){
           this.turn(90);
           this.tomaPos(this.damePos()+1);
           if(this.damePos() > 5)
                this.tomaPos(2);
    }
    
    /**
     *  Simula el mobimiento de la lava 
     */
    public void correLava(){
        Mundo mundo;
        mundo = (Mundo)getWorld();
        GreenfootImage img;
        
        if(x == 0){ 
            t = System.currentTimeMillis();
            x = 1;               
        }
        
        if(x < 8){
            if( ((System.currentTimeMillis()-t)/mundo.dameVel()) >= 1 ){
                    t = System.currentTimeMillis();
                    if(this.dameDir() == 0)
                        this.setImage(mundo.dameCurva(x));
                        
                    else 
                        this.setImage(mundo.dameCurva1(x));
                    x++;
            }
        }
        
    }
    
     /**
     * selecciona e inicializa los para metros del proximo tramo de camino, en caso de que no se 
     * tenga un camino para continuar se regresa un valor de false
     */
    public boolean correSig(){
        
        List<Camino> vecinos = this.getNeighbours(1, false, Camino.class);
        Mundo mundo;
        mundo = (Mundo)getWorld();
        
        switch(this.damePos()){
            case(2):
                for(Camino it: vecinos){
                    if( (super.dameDir() == 0 && it.getY() ==(this.getY()-1)) && (( it.damePos() == 1 || it.damePos() == 5) || it.damePos() == 4) ){
                        it.cambiaBool();
                        if(it.damePos() == 5){
                            it.tomaDir(0);
                        }
                        else{
                            it.tomaDir(1);    
                        }
                        return true;
                    }
                    if( (super.dameDir() == 1 && it.getX() == (this.getX()-1)) && ( (it.damePos() == 4 || it.damePos() == 0) || it.damePos() == 3) ){
                        it.cambiaBool();
                        if(it.damePos() == 4){
                            it.tomaDir(0);
                        }
                        else{
                            it.tomaDir(1);
                        }
                        return true;
                    }
                }
                break;
                
           case(3):
                for(Camino it: vecinos){
                    if( (super.dameDir() == 0 && it.getX() == (this.getX()+1)) && ( (it.damePos() == 5 || it.damePos() == 2) || (it.damePos() == 0 || it.damePos() == 3) ) ){    
                        it.cambiaBool();
                        if(it.damePos() == 5){
                            it.tomaDir(1);
                        }
                        else{
                            it.tomaDir(0);
                        }
                        return true;
                    }
                    
                    if( (super.dameDir() == 1 && it.getY() == (this.getY()-1)) && ( (it.damePos() == 5 || it.damePos() == 4) || it.damePos() == 1 ) ){
                        it.cambiaBool();
                        if(it.damePos() == 5){
                            it.tomaDir(0);
                        }
                        else{
                            it.tomaDir(1);
                        }
                        return true;
                    }
                }
                break;
                
           case(4):
                for(Camino it: vecinos){
                    if( (super.dameDir() == 0 && it.getY() == (this.getY()+1) ) && ( (it.damePos() == 3 || it.damePos() == 2) || it.damePos() == 1 ) ){
                        it.cambiaBool();
                        if(it.damePos() == 2){
                            it.tomaDir(1);
                        }
                        else{
                            it.tomaDir(0);
                        }
                        return true;
                    }
                    
                    if( (super.dameDir() == 1 && it.getX() == (this.getX()+1) ) && ( (it.damePos() == 5 || it.damePos() == 2) || it.damePos() == 0 ) ){
                        it.cambiaBool();
                        if(it.damePos() == 5){
                            it.tomaDir(1);    
                        }
                        else{
                            it.tomaDir(0);
                        }
                        return true;
                    }
                }
                break;
                
           case(5):
                for(Camino it: vecinos){    
                    if( (super.dameDir() == 0 && it.getX() ==(this.getX()-1)) && ( (it.damePos() == 4 || it.damePos() == 3) || it.damePos() == 0 ) ){
                        it.cambiaBool();
                        if(it.damePos() == 4){
                            it.tomaDir(0);
                        }
                        else{
                            it.tomaDir(1);
                        }
                        return true;
                    }
                    
                    if( (super.dameDir() == 1 && it.getY() == (this.getY()+1)) && ( (it.damePos() == 3 || it.damePos() == 2) || it.damePos() == 1 ) ){
                        it.cambiaBool();
                        if(it.damePos() == 2){
                            it.tomaDir(1);
                        }
                        else{
                            it.tomaDir(0);
                        }
                        return true;
                    }
                }
                break;
        }
        
        mundo.cPierde();
        return false;
    }
}
